class ListNode:
    def __init__(self, value=0, next=None):
        self.value = value
        self.next = next


# 定义一个函数可以用于找到链表的中间节点
def find_middle_node(head):
    slow = head
    fast = head

    # 定义快慢指针，每次让快指针走两步，慢指针走一步
    # 这样当快指针走到结尾的时候，慢指针刚好走到了一半
    while fast is not None and fast.next is not None:
        slow = slow.next

        # 在访问.next时候，需要注意节点是否为空
        if fast.next is not None:
            fast = fast.next.next

    return slow.value

head = ListNode(1)
head.next = ListNode(2)
head.next.next = ListNode(3)
head.next.next.next = ListNode(4)
head.next.next.next.next = ListNode(5)

print(find_middle_node(head))

head.next.next.next.next.next = ListNode(6)
print(find_middle_node(head))

